package dao;

import data.Course;
import data.Teacher;
import service.DbConnect;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class CourseDao {
    private static DbConnect<Course> c=new DbConnect();
    private static Course course=new Course();
    public static List listAll(){
        try {
            return c.selectList("select * from course",course);
        }
        catch (Exception e){
            System.out.println("错误"+e.getMessage());
            return null;
        }
    }

    public static Course one(int id){
        try{
            return c.selectOne("select * from course where id="+id,course);
        }catch (Exception e){
            System.out.println("错误"+e.getMessage());
            return null;
        }
    }

    public static boolean add(Course t) throws Exception {
        String sql="insert into course (name) values"+t.simplSql();
        return c.execute(sql);
    }

    public static boolean del(int id) throws Exception{
        String sql="delete table course where id="+id;
        return c.execute(sql);
    }
    public static boolean edit(String val,int id){
        try {
            String sql="update course set name=? where id=?";
            PreparedStatement preparedStatement = c.preparedStatement(sql);
            preparedStatement.setString(1,val);
            preparedStatement.setInt(2,id);
            c.que(preparedStatement);
            return true;
        } catch (SQLException e) {
            System.out.println("错误："+e.getMessage());
            return false;
        }
    }
}
